package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.enums.AbnormalEnum;
import com.ruoyi.framework.web.exception.TadpoleException;
import com.ruoyi.web.domain.UserEmpower;
import com.ruoyi.web.mapper.UserEmpowerMapper;
import com.ruoyi.web.service.IUserEmpowerService;
import com.ruoyi.web.utils.IdWorker;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户授权Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-03
 */
@Service
@AllArgsConstructor
public class UserEmpowerServiceImpl implements IUserEmpowerService {
    private final UserEmpowerMapper userEmpowerMapper;

    private final IdWorker idWorker;

    @Override
    public R<String> save(UserEmpower empower) {
        try {
            if (empower.getUserAddress().isEmpty()) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(), "User address cannot be empty");
            }
            if (empower.getEmpowerAddress().isEmpty()) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(), "Authorized address cannot be empty");
            }
            if (empower.getNum() == null) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(), "Authorized quantity cannot be empty");
            }
            if (empower.getChain().isEmpty()) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(), "Authorized public chain cannot be empty");
            }
            Long userId = SecurityUtils.getUserId();
            if (userId == null) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
            }
            List<UserEmpower> empowers = userEmpowerMapper.selectUserEmpowerList(new UserEmpower() {{
                setUserId(userId);
                setChain(empower.getChain());
            }});
            if (empowers.size() > 0) {
                UserEmpower userEmpower = empowers.get(0);
                userEmpower.setUserAddress(empower.getUserAddress());
                userEmpower.setChain(empower.getChain());
                userEmpower.setEmpowerAddress(empower.getEmpowerAddress());
                userEmpower.setNum(empower.getNum());
                userEmpower.setTxHash(empower.getTxHash());
                userEmpower.setUpdateTime(new Date());
                userEmpowerMapper.updateUserEmpower(userEmpower);
            } else {
                empower.setId(idWorker.nextId());
                empower.setUserId(userId);
                empower.setCreateTime(new Date());
                empower.setUpdateTime(new Date());
                empower.setEmpowerAddress(empower.getEmpowerAddress());
                userEmpowerMapper.insertUserEmpower(empower);
            }
            return R.ok("Authorization succeeded");
        } catch (TadpoleException exception) {
            throw exception;
        } catch (Exception e) {
            throw new TadpoleException(AbnormalEnum.MSG.DISCONNECTED);
        }
    }

    /**
     * 查询用户授权
     *
     * @param id 用户授权主键
     * @return 用户授权
     */
    @Override
    public UserEmpower selectUserEmpowerById(Long id) {
        return userEmpowerMapper.selectUserEmpowerById(id);
    }

    /**
     * 查询用户授权列表
     *
     * @param userEmpower 用户授权
     * @return 用户授权
     */
    @Override
    public List<UserEmpower> selectUserEmpowerList(UserEmpower userEmpower) {
        return userEmpowerMapper.selectUserEmpowerList(userEmpower);
    }

    /**
     * 新增用户授权
     *
     * @param userEmpower 用户授权
     * @return 结果
     */
    @Override
    public int insertUserEmpower(UserEmpower userEmpower) {
        userEmpower.setCreateTime(DateUtils.getNowDate());
        return userEmpowerMapper.insertUserEmpower(userEmpower);
    }

    /**
     * 修改用户授权
     *
     * @param userEmpower 用户授权
     * @return 结果
     */
    @Override
    public int updateUserEmpower(UserEmpower userEmpower) {
        userEmpower.setUpdateTime(DateUtils.getNowDate());
        return userEmpowerMapper.updateUserEmpower(userEmpower);
    }

    /**
     * 批量删除用户授权
     *
     * @param ids 需要删除的用户授权主键
     * @return 结果
     */
    @Override
    public int deleteUserEmpowerByIds(Long[] ids) {
        return userEmpowerMapper.deleteUserEmpowerByIds(ids);
    }

    /**
     * 删除用户授权信息
     *
     * @param id 用户授权主键
     * @return 结果
     */
    @Override
    public int deleteUserEmpowerById(Long id) {
        return userEmpowerMapper.deleteUserEmpowerById(id);
    }
}
